import { url } from './config.js';

function request(url, data) {
    return new Promise((resolve, reject) => {
        try {
            axios.post(url, data).then(res => {
                // console.log('res', res.data.data.url);
                resolve(res.data);
            })
        } catch (error) {
            reject(error)
        }

    })
}

//文件上传


function UploadTool() {

   var files = [];


    this.getResFiles = function () {
        return files;
    }
    
    this.dirUpload = function (e) {
        const input = document.createElement("input");
        input.type = "file";
        input.setAttribute("allowdirs", "true");
        input.setAttribute("directory", "true");
        input.style.cssText = "display:none";
        input.setAttribute("webkitdirectory", "true");
        input.multiple = true;
        document.querySelector("body").appendChild(input);
        input.click();

        input.onchange = async function (e) {
            const file = e.target["files"];
            const len = file.length;
            var count = 0;

            while (count < len) {
                if (count == len) {
                    return console.log('上传完毕')
                }
                const formData = new FormData();
                formData.append("file", file[count], file[count].webkitRelativePath)
                request(url, formData).then(res => {
                    files.push(res.data.url);
                })
                count++;
            }

            document.querySelector("body").removeChild(input);
        }
    }

    // 拖拽上传事件
    this.dropUpload = function (event) {
        var droptarget = document.getElementById(this.id);
        // 文件数组
        let fileArr = []
        // 阻止事件的默认行为
        event.preventDefault();
        if (event.type === 'drop') {
            // 文件进入并松开鼠标,文件边框恢复正常
            droptarget.style.borderColor = '#a89b9b'
            for (let file of event.dataTransfer.files) {
                // 把文件保存到文件数组中
                fileArr.push(file)
                // 初始化文件
                // filesToBlod(file)
            }
        } else if (event.type === 'dragleave') {
            // 离开时边框恢复
            droptarget.style.borderColor = '#a89b9b'
        } else {
            // 进入边框变为红色
            droptarget.style.borderColor = 'red'
        }
        // console.log('fileArr', fileArr)
        let file = fileArr[0];
        var params = new FormData();
        params.append('file', file)
        request(url, params).then(res => {
            files.push(res.data.url)
        })

    }

    this.fileUpolod = function () {
        const input = document.createElement("input");
        input.type = "file";
        input.setAttribute("allowdirs", "true");
        input.setAttribute("directory", "true");
        input.style.cssText = "display:none";
        input.multiple = true;
        document.querySelector("body").appendChild(input);
        // todo 这里增加了input标签，可以给它删掉
        input.click();
        input.onchange = (e) => {
            let file = e.target["files"][0];
            console.log(e.target["files"])
            var params = new FormData();
            params.append('file', file)
            request(url, params).then(res => {
                files.push(res.data.url);
            })
            document.querySelector("body").removeChild(input);
        }
    }
}

const uploadTool = new UploadTool();
const fileUpolod = uploadTool.fileUpolod;
const dropUpload = uploadTool.dropUpload;
const dirUpload = uploadTool.dirUpload;
export {
    uploadTool,
    dirUpload,
    dropUpload,
    fileUpolod
}